﻿
module Sustainalytics.Governance {
    import EsgUtility = Sustainalytics.EsgRatings.EsgUtility;
    export class GovernanceIssuesController {

        private govProfile: GovernanceProfileController;
        public currentCompanyId: string;
        public overalPerformance: GovernanceCompanyOverallDto;
        public issues: CompanyKeyGovernanceIssueDto[];
        public companyAssesment: string;
        public regionalPercentileOrdinal: string;
        public outlookAssement: string;
        private selectedIssueAnchor: string;

        constructor(
            public $scope: Company.ICompanyBasedScope,
            public $rootScope: ng.IScope,
            public $q: ng.IQService,
            private $location: ng.ILocationService,
            private $anchorScroll: any,
            public govService: GovernanceCompanyProfileService,
            private NumberUtilityService: Common.NumberUtilityService
        ) {
            this.govProfile = this.$scope['governancectrl'];
            this.currentCompanyId = this.govProfile.company.id;
            this.govProfile.issueSelect = null;
            this.selectedIssueAnchor = this.govProfile.selectedIssueAnchor;
            this.init();
        }

        private init(): void {

            var ctx = this;
            var cgIssuePromise = this.govService.getCompanyCgIssues(this.currentCompanyId);
            var cgOverallPromise = this.govService.getCompanyCgOverallPerformance(this.currentCompanyId);
            var arr = [cgIssuePromise, cgOverallPromise];
            ctx.$rootScope['loading'] = true;
            this.$q.all(arr).then(results => {
                ctx.issues = results[0];
                ctx.overalPerformance = results[1];
                for (var i = 0; i < ctx.issues.length; i++) {
                    ctx.issues[i].issue.outlookAssesment = EsgUtility.getEsgOutlookTypeName(ctx.issues[i].issue.outlook);
                    ctx.issues[i].issue.assementStr = EsgUtility.getEsgPositionName(ctx.issues[i].issue.assesmentType);
                }
                ctx.companyAssesment = EsgUtility.getEsgPositionName(ctx.overalPerformance.assesment);
                ctx.outlookAssement = EsgUtility.getEsgOutlookTypeName(ctx.overalPerformance.outlook);
                ctx.regionalPercentileOrdinal = ctx.NumberUtilityService.getOrdinalNameEnd(ctx.overalPerformance.regionalPercentile);
                var jj = this.govProfile.selectedIssueAnchor;
                ctx.scroolToAnchor(ctx.selectedIssueAnchor);
            }).catch(a => {
                console.log(a);
            }).finally(() => {
                ctx.$rootScope['loading'] = false;
            });
        }

        private scroolToAnchor(anchor: string) {
            if (!anchor || anchor == '')
                return;
            var ctx = this;
            ctx.$anchorScroll.yOffset = 140;
            if (ctx.$location.hash() !== anchor) {                
                ctx.$location.hash(anchor);
            } else {                
                ctx.$anchorScroll();
            }
        }

        private switchView(selected: IndicatorDataDto): void {
            this.govProfile.issueSelect = selected;
            this.govProfile.changeView(this.govProfile.getViewNames().Indicators);

            //console.log(selected);
        }

        

        
    }
    angular.module('Sustainalytics.Governance').controller('GovernanceIssuesController', ['$scope', '$rootScope', '$q', '$location', '$anchorScroll', 'GovernanceCompanyProfileService','NumberUtilityService', GovernanceIssuesController]);

}